/**
 * 质控病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0
};

SdsQcentity.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    //查询质控项目
    //清空右边文本框的值
    $("#tipsTimDr").keywords("clearAllSelected");
    $(':input').val('');
    $("#tipsTimDr").keywords('select',SdsQcentityTips.firstId);
};

/**
 * 诊疗内容提示管理初始化
 */
var SdsQcentityTips = {
    id: "SdsQcentityTipsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    firstId: null
};

/**
 * 检查是否选中
 */
SdsQcentityTips.check = function () {
    if (SdsQcentity.entityId) {
        return true;
    } else {
        Sys.info("请先选中某一病种！");
        return false;
    }
};

/**
 * 关键字点击事件
 */
SdsQcentityTips.keywordsClick = function (v){
    $(':input').val('');
    var queryData = {"entityId": SdsQcentity.entityId,"tipsTimDr": v.id};
    //提交信息
    var ax = new $ax(Sys.backendPath + "/sds/sdsqcentitytips/tipsinfo", function (data) {
            $("#id").val(data.id);
            $("#entityId").val(data.entityId);
            $("#tipDays").val(data.tipsDays);
            $("#tipsRate").combobox("setValue", data.tipsRate);
            $("#handStop").checkbox("setValue", data.handStop !== 1 ? false : true);
            $("#linkExp").combobox("setValue", data.linkExp);
            $("#expParam").val(data.expParam);
            $("#tipsContent").val(data.tipsContent);
    },function (data){});
    ax.set(queryData);
    ax.start();
}

/**
 * 验证数据是否为空
 */
SdsQcentityTips.validate = function () {
    var errinfo = "";
    if ($("#tipsTimDr").keywords("getSelected")[0].id==="") {
        errinfo = errinfo + "请选择提示时机!<br>";
    }
    if ($("#tipDays").val()=="") {
        errinfo = errinfo + "请填写提示天数!<br>";
    }
    if ($("#tipsRate").combobox("getValue")=="") {
        errinfo = errinfo + "请选择提示频率!<br>";
    }
    if (errinfo) {
        $.messager.alert("错误提示", errinfo, 'info');
        return false;
    }
    return true;
}

/**
 * 打开查看诊疗内容提示详情
 */
SdsQcentityTips.edit = function (id) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        flag = true;
    }
    if (flag) {
        if (!this.validate()) {
            return;
        }
        var queryData = {};
        queryData['id'] = $("#id").val()!==null&&$("#id").val()!==""?$("#id").val():null;
        queryData['entityId'] = $("#entityId").val()!==null&&$("#entityId").val()!==""?$("#entityId").val():SdsQcentity.entityId;
        queryData['tipsTimDr'] = $("#tipsTimDr").keywords("getSelected")[0].id;
        queryData['tipsDays'] = $("#tipDays").val();
        queryData['tipsRate'] = $("#tipsRate").combobox("getValue");
        queryData['linkExp'] = $("#linkExp").combobox("getValue");
        queryData['expParam'] = $("#expParam").val();
        queryData['tipsContent'] = $("#tipsContent").val();
        queryData['handStop'] = $("#handStop").checkbox("getValue")?1:0;
        //提交信息
        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcentitytips/edit", function (res) {
            Sys.success(res.message);
        }, function (data) {
            Sys.error("添加失败!" + res.message + "!");
        });
        ajax.set(queryData);
        ajax.start();
    }
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/tree");
    ztree.bindOnClick(SdsQcentity.onClickEntity);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree','#seachKey',null,true);

    var times = $.parseJSON($("#times").val());
    var rates = $.parseJSON($("#rates").val())
    var exps = $.parseJSON($("#exps").val())
    SdsQcentityTips.firstId = times[0].id;
    $("#tipsTimDr").keywords({
        singleSelect: true,
        items: times,
        onClick: SdsQcentityTips.keywordsClick
    });
    $("#tipsRate").combobox({
        data: rates,
        valueField: "id",
        textField: "text"
    });
    $("#linkExp").combobox({
        data: exps,
        valueField: "id",
        textField: "text"
    });
});
